"""\
Extract macros and journal names from IEEEfull.bib

Copyright (c) 2007 Kjell Magne Fauske
See LICENSE for details.
"""

# Example data
# % aerospace and military
# @STRING{IEEE_J_AES        = "{IEEE} Transactions on Aerospace and Electronic Systems"}
# @STRING{IEEE_J_ANE        = "{IEEE} Transactions on Aerospace and Navigational Electronics"}
# @STRING{IEEE_J_ANNE       = "{IEEE} Transactions on Aeronautical and Navigational Electronics"}
# @STRING{IEEE_J_AS         = "{IEEE} Transactions on Aerospace"}
# @STRING{IEEE_J_AIRE       = "{IEEE} Transactions on Airborne Electronics"}
# @STRING{IEEE_J_MIL        = "{IEEE} Transactions on Military Electronics"}
#
# % autos, transportation and vehicles (non-aerospace)
# @STRING{IEEE_J_ITS        = "{IEEE} Transactions on Intelligent Transportation Systems"}
# @STRING{IEEE_J_VT         = "{IEEE} Transactions on Vehicular Technology"}
# @STRING{IEEE_J_VC         = "{IEEE} Transactions on Vehicular Communications"}

#^@STRING\{(.*?)\s*=\s*"(.*?)"\}$

import re
journalext_re = re.compile(r'^@STRING\{(.*?)\s*=\s*"(.*?)"\}$',re.MULTILINE)

bibsource = "d:/pycode/bibconverter/data/IEEEfull.bib"
dest = "d:/pycode/bibconverter/ieeedata.py"

if __name__ == '__main__':
    # load IEEEfull.bib
    f = open(bibsource,'r')
    data = f.read()
    f.close()
    # apply a regular expression to extract relevant data
    matches = journalext_re.finditer(data)
    journals = {}
    # create a dictionary with full name as key and maco name as value
    for m in matches:
        fullname = m.group(2)
        macro = m.group(1)
        # delete curly brackets {IEEE}
        fullname = fullname.replace('{','')
        fullname = fullname.replace('}','')
        journals[fullname] = macro
    # generate a python source file for importing data into biblib
    f = open(dest,'w')
    f.write("# This file is generated by jourext.py\n\n")
    f.write('ieeejournals = {\n')
    for (key,value) in journals.items():
        f.write('  "%s" : "%s", \n' % (key, value))
    f.write('}\n')
    f.close()

    